package com.elite.groovy.callback
import groovy.json.JsonSlurper
import groovy.json.StringEscapeUtils
import groovy.swing.impl.DefaultAction
import groovy.xml.Namespace
import java.text.MessageFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

import com.elite.groovy.util.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher
import java.util.regex.Pattern

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

import com.wisdge.dataservice.Result;
import com.elite.groovy.util.Util;


public class CcodRecordPath implements ServiceBackInvoke
{
    private static final Log log = LogFactory.getLog(CcodRecordPath.class);
    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response)
    {

        response.setCharacterEncoding("UTF-8");
        log.debug("start CcodRecordPath-----------------");
        log.debug("request--:" + request);

        String getMethod = request.getMethod();
        String callJson = "";
        BufferedReader iny = null;

        def reJson = "";

        log.debug("getMethod:" + getMethod);
        if (getMethod.equals("GET"))
        {
            callJson = request.getParameter("data");
            log.debug("原始Get请求参数:" + callJson);
        }
        else
        {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null)
            {
                callJson += line;
            }
            log.debug("原始Post请求参数:" + callJson);
        }
        try
        {
        	JSONObject inObject = JSONObject.fromObject(callJson);
			log.debug("处理前的inObject：" + inObject.toString());

			String recordHost = GetJV(inObject, "recordHost");
			String recordParam = GetJV(inObject, "recordParam");
			String recordUrl = GetJV(inObject, "recordUrl");
            log.debug("解析前recordUrl:" + recordUrl);
            
            recordUrl=URLEncoder.encode(recordUrl,"UTF-8");
            log.debug("处理后recordUrl:" + recordUrl);
            
            recordUrl=recordHost+recordUrl+recordParam;
            log.debug("拼接后recordUrl:" + recordUrl);
            
            def recordPath = Util.doHttpGet(recordUrl, "UTF-8", 120000, 0);
            
            String str=recordPath.replace("<recordFiles><recordFile><recordName>", "");
    		String str1=str.replace("</recordName></recordFile></recordFiles>", "");
    		log.debug("str1:" + str1);
    		str1=str1.replaceAll("\r|\n","");
            //成功返回
             reJson = "{\"code\":\"1\",\"message\":\""+str1+"\"}";
            
            log.debug("recordPath返回Json:" + reJson);
            return reJson;
        }
        catch(Exception e)
        {
            log.debug("接口处理异常" + e.getMessage());
            //失败返回
            reJson = "{\"code\":\"0\",\"description\":\"错误:" + e.getMessage() + "\"}";
            log.debug("recordPath返回Json:" + reJson);
            return reJson;
        }
    }
//  解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}


}
